#region License
///License Begin
///This program has been developed by: Rodolfo Grave de Peralta, 
///Copyright (C) 2007 Rodolfo Grave de Peralta
///This program is free software; you can redistribute it and/or modify
///it under the terms of the GNU General Public License as published by
///the Free Software Foundation; either version 2 of the License, or
///(at your option) any later version.
///
///This program is distributed in the hope that it will be useful,
///but WITHOUT ANY WARRANTY; without even the implied warranty of
///MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
///GNU General Public License for more details.
///
///You should have received a copy of the GNU General Public License
///along with this program; if not, write to the Free Software
///Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
///License End
#endregion

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace TEAM.TimeTracking.Model
{
    public class User : INotifyPropertyChanged
    {

        public delegate void TaskExecutionStartedEventHandler(User sender, TaskExecutionStartedEventArgs e);

        public event TaskExecutionStartedEventHandler TaskExecutionStarted;

        public User()
        {
            tasks = new ObservableCollection<Task>();
            history = new ObservableCollection<TaskExecution>();
            projects = new ObservableCollection<Project>();
        }

        protected ObservableCollection<TaskExecution> history;
        protected ReadOnlyObservableCollection<TaskExecution> readonlyHistory;

        public ReadOnlyObservableCollection<TaskExecution> History
        {
            get 
            {
                if (readonlyHistory == null)
                {
                    readonlyHistory = new ReadOnlyObservableCollection<TaskExecution>(history);
                }
                return readonlyHistory; 
            }
        }

        public void RemoveTaskExecution(TaskExecution t)
        {
            if (t == CurrentTaskExecution)
            {
                OnPropertyChanged("CurrentTaskExecution");
            }
            history.Remove(t);
        }

        protected ObservableCollection<Task> tasks;

        public ObservableCollection<Task> Tasks
        {
            get { return tasks; }
        }

        protected ObservableCollection<Project> projects;

        public ObservableCollection<Project> Projects
        {
            get { return projects; }
        }

        protected TaskExecution CurrentTaskExecution
        {
            get
            {
                TaskExecution result = null;
                if (history.Count > 0)
                {
                    result = history[history.Count - 1];
                }
                return result;
            }
        }

        public bool IsExecutingTask
        {
            get { return CurrentTaskExecution != null; }
        }

        public Task CurrentTask
        {
            get { return CurrentTaskExecution.Task; }
        }

        public DateTime CurrentTaskStartTime
        {
            get { return CurrentTaskExecution.StartTime; }
        }

        public DateTime? CurrentTaskEndTime
        {
            get { return CurrentTaskExecution.EndTime; }
        }

        public string CurrentTaskDescription
        {
            get { return CurrentTaskExecution.Description; }
        }

        public void EndCurrentTask()
        {
            if (CurrentTaskExecution != null)
            {
                CurrentTaskExecution.EndTime = DateTime.Now;
            }
        }

        public void StartTask(Task t, DateTime startTime, string description)
        {
            EndCurrentTask();
            TaskExecution newTask = new TaskExecution(t, DateTime.Now);
            newTask.Description = String.IsNullOrEmpty(description) ? null : description;
            history.Add(newTask);
            OnPropertyChanged("CurrentTask");
            OnPropertyChanged("CurrentTaskStartTime");
            OnPropertyChanged("CurrentTaskDescription");
            OnTaskExecutionStarted(newTask);
        }

        public void StartTaskNow(Task t, string description)
        {
            StartTask(t, DateTime.Now, description);
        }

        protected Task defaultTaskWhenLeaving;

        public Task DefaultTaskWhenLeaving
        {
            get { return defaultTaskWhenLeaving; }
            set { defaultTaskWhenLeaving = value; }
        }

        protected Task defaultTaskWhenArriving;

        public Task DefaultTaskWhenArriving
        {
            get { return defaultTaskWhenArriving; }
            set { defaultTaskWhenArriving = value; }
        }

        protected string name;

        public string Name
        {
            get { return name; }
            set 
            { 
                name = value;
                OnPropertyChanged("Name");
            }
        }

        protected string id;

        public string Id
        {
            get { return id; }
            set { id = value; }
        }

        protected void OnTaskExecutionStarted(TaskExecution t)
        {
            if (TaskExecutionStarted != null)
            {
                TaskExecutionStarted(this, new TaskExecutionStartedEventArgs(t));
            }
        }

        protected virtual void OnPropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }

        #region Miembros de INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        public void ContinueCurrentTask()
        {
            if (CurrentTaskExecution.EndTime != null)
            {
                CurrentTaskExecution.EndTime = null;
            }
            OnTaskExecutionStarted(CurrentTaskExecution);
        }
    }
}
